const mapBuild = function (
  dom: string,
  urlStr: string,
  layerName: string,
  point: any,
  level: number,
  callBack: Function
) {
  console.log("mapBuild dom", dom);
  const master = {
    mapDiv: dom || {},
    map: {},
    layer: {},
    vector: {} as any,
    markers: {},
    pointMarkers: {},
  };
  function init() {
    master.map = new (window as any).SuperMap.Map(master.mapDiv, {
      numZoomLevels: 6,
      maxScale: 0.00004,
      minScale: 0.000004,
    });
    const layerNames = [
      "China_Vegetation_R@China400",
      "China_Hyd_R@China400",
    ].join(","); //解决请求symbol问题
    const cartoCss = `
                // #World_Continent_txt__China400::symbol0{text-opacity:0;}
               // #World_Ocean_txt__China400::symbol0{text-opacity:0;}
               // #haikou__China::symbol0{text-opacity:0;}
               // #chiweiyu__China::symbol0{text-opacity:0;}
               // #chiweiyu_1__China::symbol0{text-opacity:0;}
               // #xingkaihu_A__China::symbol0{text-opacity:0;}
               // #xingkaihu_B__China::symbol0{text-opacity:0;}
               // #xingkaihu_C__China::symbol0{text-opacity:0;}
               // #China_provincename_B_txt__China::symbol0{text-opacity:0;}
               // #China_provincename_A_txt__China::symbol0{text-opacity:0;}
               #China_Vegetation_R__China400::symbol0{polygon-fill:rgb(12, 18, 37);line-opacity:0;}
               #China_Hyd_R__China400::symbol0{polygon-fill:rgb(12, 18, 37);line-opacity:0;}
               // #River__China::symbol0{polygon-fill:rgb(217, 22, 23);line-opacity:0;}
               // #China_Hydside_R__China400::symbol0{polygon-fill:rgb(217, 22, 23);line-opacity:0;}
               // #Hydside_Area_pl__Hydside::symbol0{polygon-fill:rgb(217, 22, 23);polygon-opacity:0;line-opacity:0;polygon-comp-op:lighten;}
               // #China_Province_R__China400::symbol0{polygon-fill:rgb(217, 22, 23);line-opacity:0;}
               // #World_Continent__China400::symbol0{polygon-fill:rgb(217, 22, 23);line-opacity:0;}
               // #World_Division__China400::symbol0{polygon-fill:rgb(217, 22, 23);line-opacity:0;polygon-comp-op:lighten;}
               `;
    master.layer = new (window as any).SuperMap.Layer.TiledVectorLayer(
      layerName,
      urlStr,
      {
        transparent: true,
        cacheEnabled: true,
        layerNames: "[" + layerNames + "]",
      },
      { useLocalStorage: true, cartoCss: cartoCss }
    );
    master.vector = new (window as any).SuperMap.Layer.Vector("vector");
    master.markers = new (window as any).SuperMap.Layer.Markers("markers");
    master.pointMarkers = new (window as any).SuperMap.Layer.Markers(
      "pointMarkers"
    );
    (master.layer as any).events.on({
      ["layerInitialized"]: function () {
        return addLayer();
      },
    });
    const control = (master.map as any).getControlsByClass(
      "(window as any)SuperMap.Control.PanZoomBar"
    );
    for (let i = 0; i < control.length; i++) {
      if (control[i]) {
        control[i].destroy();
      }
    }
    const selectFeature = new (window as any).SuperMap.Control.SelectFeature(
      master.vector,
      {}
    );
    (master.map as any).addControl(selectFeature);
    selectFeature.activate();
  }
  /**添加图层*/
  function addLayer() {
    console.log("mapBuild 添加图层", master);
    (master.map as any).addLayers([
      master.layer,
      master.vector,
      master.pointMarkers,
      master.markers,
    ]);
    let lonLatPoint;
    if (master.mapDiv != "trainMap") {
      lonLatPoint = bd09_To_Gcj02(point.x, point.y);
    } else {
      lonLatPoint = point;
    }
    const lonLat = new (window as any).SuperMap.LonLat(
      lonLatPoint.x,
      lonLatPoint.y
    );
    (master.map as any).setCenter(lonLat, level);
    callBack();
  }
  /**删除图层*/
  // function removeLayer(layer) {
  //   master.map.removeLayer(layer);
  // }
  /**添加线路矢量要素*/
  function addLineVector(points: any, stroke: any) {
    console.log("添加线路矢量要素");
    const line = new (window as any).SuperMap.Geometry.LineString(points);
    const lineVector = new (window as any).SuperMap.Feature.Vector(line);
    lineVector.style = {
      strokeColor: stroke,
      strokeWidth: 2,
    };
    master.vector.addFeatures(lineVector);
  }
  /** 添加单点覆盖物 */
  // function addPointVector(point: any, fill: any) {
  //   var pointVector = new (window as any).SuperMap.Feature.Vector(point);
  //   pointVector.style = {
  //     fillColor: fill,
  //     strokeColor: fill,
  //     pointRadius: 4,
  //   };
  //   master.vector.addFeatures(pointVector);
  // }
  /** 删除线覆盖物 */
  // function clearLine() {
  //   master.vector.removeAllFeatures();
  // }
  /** 百度坐标转换位中国标准坐标。
   * point:目标点经纬度*/
  function bd09_To_Gcj02(bd_lon: number, bd_lat: number) {
    var pi = 3.1415926535897932384626;
    var x = bd_lon - 0.0065;
    var y = bd_lat - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
    var gg_lon = z * Math.cos(theta);
    var gg_lat = z * Math.sin(theta);
    return new (window as any).SuperMap.Geometry.Point(gg_lon, gg_lat);
  }
  init();
  return {
    master,
    addLineVector,
    // addPointVector,
    // clearLine,
    bd09_To_Gcj02,
  };
};
export default mapBuild;
